import cv2

# 图片路径
img_path = "avatar.png"  # 修改为你的图片路径
img = cv2.imread(img_path)
clone = img.copy()
bbox = []

def draw_rectangle(event, x, y, flags, param):
    global bbox, clone
    if event == cv2.EVENT_LBUTTONDOWN:
        bbox = [(x, y)]  # 左上角
    elif event == cv2.EVENT_LBUTTONUP:
        bbox.append((x, y))  # 右下角
        cv2.rectangle(clone, bbox[0], bbox[1], (0, 255, 0), 2)
        cv2.imshow("image", clone)
        print("Selected bbox:", bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1])

cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_rectangle)

while True:
    cv2.imshow("image", clone)
    key = cv2.waitKey(1) & 0xFF
    if key == 27:  # 按 ESC 键退出
        break

cv2.destroyAllWindows()

